var _ROOTURI_ = "/scheduler";
dojo.require("dojo.parser");

function display_error(error_type, div)
{
  setInnerHTML(error_type, div);
}

function setInnerHTML(content, dom_node, skip_reparse)
{
  dom_node = dojo.byId(dom_node);
  //alert("Setting inner html on "+dom_node);
  dojo.query('[widgetId]', dom_node).forEach(function(n) {
    dijit.byNode(n).destroy();
  });
  dom_node.innerHTML = content;

  if(skip_reparse){}
  else
  {
    dojo.parser.parse(dom_node);
  }
}

function doLoading(type)
{
  if(type == "topbar") // For the top, fixes loading image being out of bounds
    setInnerHTML("<img src=\""+_ROOTURI_+"/images/loading2.gif\" width=\"16\" height=\"16\" alt=\"\" />", "topbar_div"); // Uses loading2.gif (vs. loading.gif for other DIVs)
  else
    setInnerHTML("<div class=\"load\"><img src=\""+_ROOTURI_+"/images/loading.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>", type+"_div");
}

function doLoading2(type)
{
  (type) ? dojo.byId('boxload').style.display = 'block' : dojo.byId('boxload').style.display = 'none';
}

function link_to(url, text)
{
  return "<a href=\"#\" onclick=\"goto_page("+url+")\">"+text+"</a>";
}

function reload_topbar()
{
  //alert("Reloading Topbar");
  doLoading("topbar");
  dojo.xhrGet({
    url: _ROOTURI_+"/userstatus",
    handleAs: "json",
    //preventCache: true,
    timeout: 5000,
    load: function(response, ioArgs){
      //should return {loggedIn: boolean, username: string, hasSchedule: boolean, hasAdmin: boolean, urls{login: string, logout: string, lostpass: string, changepass: string, admin: string, schedule: string}}
      if(response.loggedIn)
      {
        var ihtm = "<strong>"+response.username+"</strong> - "+link_to(response.urls.logout, "<span>logout</span>")+" "+link_to(response.urls.changepass, "<span>change password</span>");
        if(response.hasSchedule)
        {
          ihtm = ihtm+" "+link_to(response.urls.schedule);
        }
        if(response.hasAdmin)
        {
          ihtm = ihtm+" "+link_to(response.urls.admin);
        }
        setInnerHTML(ihtm, "topbar_div");
      }
      else
      {
        setInnerHTML("<strong>Welcome, Guest</strong> - "+link_to(response.urls.login, "<span>login</span>")+" "+link_to(response.urls.lostpass, "<span>lost password</span>"), "topbar_div");
      }
      return response;
    },
    error: function(response, ioArgs){
      display_error("-- Loading Error --", "topbar_div");
      return response;
    }
  });
}

function goto_page(the_url, the_target)
{
  if(!the_target)
  {
    the_target = "main_div";
  }
  doLoading("main");
  dojo.xhrGet({
    url: the_url,
    handleAs: "json",
    //preventCache: true,
    sync: true,
    timeout: 5000,
    load: function(response, ioArgs){
      //should return {callback: string, data: object}
      if(response.callback)
      {
        window[response.callback](response.data);
      }
      else
      {
        display_error("-- Loading Error --", the_target);
      }
      return response;
    },
    error: function(response, ioArgs){
      display_error("-- Loading Error --", the_target);
      return response;
    }
  });
}
